package com.chacha.utils;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Vector;

import com.jcraft.jsch.Channel;
import com.jcraft.jsch.ChannelSftp;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.Session;

public class SFTPClient {
	private String sftpHost ;
	private int    sftpPort ;
	private String sftpUser ;
	private String sftpPass ;
	
	public SFTPClient(){
		NimConfig cfg = new NimConfig();
		
		setSftpHost(cfg.getProperty("sftp.host"));
		setSftpPort(cfg.getIntProperty("sftp.port"));
		setSftpUser(cfg.getProperty("sftp.user"));
		setSftpPass(cfg.getProperty("sftp.pass"));
		
	}
	public SFTPClient(String host, String port, String user, String pass){
		int portInt = 22;
		try {
			portInt = Integer.parseInt(port);
		} catch (Exception e) {
			// ignore
		}
		
		
		setSftpHost(host);
		setSftpPort(portInt);
		setSftpUser(user);
		setSftpPass(pass);		
	}
		
	
	public boolean sendFile(File file, String sourceFolder){
		 
		Session     session     = null;
		Channel     channel     = null;
		ChannelSftp channelSftp = null;
		
		try{
            JSch jsch = new JSch();
            session = jsch.getSession(sftpUser,sftpHost,sftpPort);
            System.out.println("Connecting to server... ");
            
            session.setPassword(sftpPass);
            java.util.Properties config = new java.util.Properties();
            config.put("StrictHostKeyChecking", "no");
            session.setConfig(config);
            session.connect();
            channel = session.openChannel("sftp");
            channel.connect();
            channelSftp = (ChannelSftp)channel;
            channelSftp.cd(sourceFolder);
            
            channelSftp.put(new FileInputStream(file), file.getName());
            
            return true;
			}catch(Exception ex){
				ex.printStackTrace();
				return false;
			}
	}
	
	public boolean sendFile(InputStream inputStream, String sourceFolder, String fileName){
		 
		Session     session     = null;
		Channel     channel     = null;
		ChannelSftp channelSftp = null;
		
		try{
            JSch jsch = new JSch();
            session = jsch.getSession(sftpUser,sftpHost,sftpPort);
            System.out.println("Connecting to server... ");
            
            session.setPassword(sftpPass);
            java.util.Properties config = new java.util.Properties();
            config.put("StrictHostKeyChecking", "no");
            session.setConfig(config);
            session.connect();
            channel = session.openChannel("sftp");
            channel.connect();
            channelSftp = (ChannelSftp)channel;
            channelSftp.cd(sourceFolder);
            
            channelSftp.put(inputStream, fileName);
            
            return true;
			}catch(Exception ex){
				ex.printStackTrace();
				return false;
			}
	}
	
	public File downloadFile(String fileName, String destinationFolder, String downloadFolder){
		 
		Session     session     = null;
		Channel     channel     = null;
		ChannelSftp channelSftp = null;
		try{
            JSch jsch = new JSch();
            session = jsch.getSession(sftpUser,sftpHost,sftpPort);
            System.out.println("Connecting to server... ");
            
            session.setPassword(sftpPass);
            java.util.Properties config = new java.util.Properties();
            config.put("StrictHostKeyChecking", "no");
            session.setConfig(config);
            session.connect();
            channel = session.openChannel("sftp");
            channel.connect();
            channelSftp = (ChannelSftp)channel;
            channelSftp.cd(destinationFolder);
            
            byte[] buffer = new byte[1024];
            BufferedInputStream bis = new BufferedInputStream(channelSftp.get(fileName));
            
            File file = new File( downloadFolder + fileName);
            OutputStream os = new FileOutputStream(file);
            BufferedOutputStream bos = new BufferedOutputStream(os);
            int readCount;
            
            while( (readCount = bis.read(buffer)) > 0) {
            	System.out.println("Writing: " );
            	bos.write(buffer, 0, readCount);
            	}
            	bis.close();
            	bos.close();
            	//Remove File from server after download
            	//channelSftp.rm(fileName);
            return file;
		}catch(Exception ex){
			ex.printStackTrace();
			return null;
		}
		
	}
	
	public void deleteFile(String fileName, String destinationFolder, String downloadFolder){
		 
		Session     session     = null;
		Channel     channel     = null;
		ChannelSftp channelSftp = null;
		try{
            JSch jsch = new JSch();
            session = jsch.getSession(sftpUser,sftpHost,sftpPort);
            System.out.println("Connecting to server... ");
            
            session.setPassword(sftpPass);
            java.util.Properties config = new java.util.Properties();
            config.put("StrictHostKeyChecking", "no");
            session.setConfig(config);
            session.connect();
            channel = session.openChannel("sftp");
            channel.connect();
            channelSftp = (ChannelSftp)channel;
            channelSftp.cd(destinationFolder);
            channelSftp.rm(fileName);
            	//Remove File from server after download
            	//channelSftp.rm(fileName);
		}catch(Exception ex){
			ex.printStackTrace();
		}
		
	}
	
	public boolean testFolder( String sourceFolder){
		 
		Session     session     = null;
		Channel     channel     = null;
		ChannelSftp channelSftp = null;
		
		try{
            JSch jsch = new JSch();
            session = jsch.getSession(sftpUser,sftpHost,sftpPort);
            session.setPassword(sftpPass);
            java.util.Properties config = new java.util.Properties();
            config.put("StrictHostKeyChecking", "no");
            session.setConfig(config);
            session.connect();
            channel = session.openChannel("sftp");
            channel.connect();
            channelSftp = (ChannelSftp)channel;
            channelSftp.cd(sourceFolder);
            
            return true;
			}catch(Exception ex){
				ex.printStackTrace();
				return false;
			}
	}
	
	public boolean testConnection(){
		Session     session     = null;
		Channel     channel     = null;
		try{
            JSch jsch = new JSch();
            session = jsch.getSession(sftpUser,sftpHost,sftpPort);
            session.setPassword(sftpPass);
            java.util.Properties config = new java.util.Properties();
            config.put("StrictHostKeyChecking", "no");
            session.setConfig(config);
            session.connect();
            channel = session.openChannel("sftp");
            channel.connect();
           
            return true;
		}catch(Exception ex){
			ex.printStackTrace();
		return false;
		}
	}
	
	/**
	 * @param sourceFolder 
	 * @param destinationFolder
	 */
	public boolean uploadFiles(String sourceFolder, String destinationFolder){
		 
		Session     session     = null;
		Channel     channel     = null;
		ChannelSftp channelSftp = null;
		 
		try{
            JSch jsch = new JSch();
            session = jsch.getSession(sftpUser,sftpHost,sftpPort);
            System.out.println("Connecting to server... " );
            
            session.setPassword(sftpPass);
            java.util.Properties config = new java.util.Properties();
            config.put("StrictHostKeyChecking", "no");
            session.setConfig(config);
            session.connect();
            channel = session.openChannel("sftp");
            channel.connect();
            channelSftp = (ChannelSftp)channel;
            channelSftp.cd(destinationFolder);
            
            File dir = new File(sourceFolder); 
            
            for(File file:dir.listFiles()){
            	channelSftp.put(new FileInputStream(file), file.getName());            	
            	System.out.println("Uploading file "+file.getName()); 
            	file.deleteOnExit();
            } 
            
            channelSftp.disconnect();
            channelSftp.quit();
            
            return true;
			}catch(Exception ex){
				ex.printStackTrace();
				return false;
			}
	}
	
	/**
	 * @param sftpFolder
	 * @param localFolder
	 */
	public void downloadFiles( String sftpFolder, String localFolder){
		 
		Session     session     = null;
		Channel     channel     = null;
		ChannelSftp channelSftp = null;
		try{
            JSch jsch = new JSch();
            session = jsch.getSession(sftpUser,sftpHost,sftpPort);
            System.out.println("Connecting to server... " );
            
            session.setPassword(sftpPass);
            java.util.Properties config = new java.util.Properties();
            config.put("StrictHostKeyChecking", "no");
            session.setConfig(config);
            session.connect();
            channel = session.openChannel("sftp");
            channel.connect();
            channelSftp = (ChannelSftp)channel;
            channelSftp.cd(sftpFolder);
            Vector files = channelSftp.ls(sftpFolder);
            for (int i=0; i<files.size(); i++) {
            	com.jcraft.jsch.ChannelSftp.LsEntry lsEntry =(com.jcraft.jsch.ChannelSftp.LsEntry) files.get(i);
            	
            	if (!lsEntry.getFilename().equals(".") && !	lsEntry.getFilename().equals("..")) {
            			System.out.println("Downloading file "+lsEntry.getFilename());            			
            			
            			String outputFileName = localFolder+lsEntry.getFilename();
            			//Get the file and write it to our local file	system
            			File f = new	File(outputFileName);
            			channelSftp.get(lsEntry.getFilename(), new FileOutputStream(f));
            			//Remove the file from the server
            			//channelSftp.rm(lsEntry.getFilename());
            	}
            }
            
            
		}catch(Exception ex){
			ex.printStackTrace();
		}
		
	}

	public String getSftpHost() {
		return sftpHost;
	}

	public void setSftpHost(String sftpHost) {
		this.sftpHost = sftpHost;
	}

	public int getSftpPort() {
		return sftpPort;
	}

	public void setSftpPort(int sftpPort) {
		this.sftpPort = sftpPort;
	}

	public String getSftpUser() {
		return sftpUser;
	}

	public void setSftpUser(String sftpUser) {
		this.sftpUser = sftpUser;
	}

	public String getSftpPass() {
		return sftpPass;
	}

	public void setSftpPass(String sftpPass) {
		this.sftpPass = sftpPass;
	}

		
	

}
